package com.bloveoacom.util;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;

import com.bloveoacom.base.AppContext;
import com.bloveoacom.weichi.R;

public class LoadDialogUtil {

    static ProgressDialog pd;

    /**
     * 全局等待对话框
     */
    public static void showWaitDialog(Context context) {
        showWaitDialog(context,context.getString(R.string.loading));

    }

    /**
     * 全局等待对话框
     */
    public static void showWaitDialog(Context context,final String remind) {
        showWaitDialog(context,remind,true);
    }

    /**
     * 全局等待对话框
     */
    public static void showWaitDialog(final Context mContext, final String remind, final boolean isCancel) {
        //如果ctx等于空或者isFinishing
        if (mContext == null)
            return;
        if(mContext instanceof Activity){
            if(((Activity) mContext).isFinishing()){
                return;
            }
        }
        if(pd!=null){
            dismissWaitDialog();
        }
        AppContext.mHandler.post(new Runnable() {
            @Override
            public void run() {
                if (pd == null
                        || !pd.isShowing()) {
                    pd = new ProgressDialog(mContext);
                    pd.setMessage(remind);
                    pd.setCancelable(isCancel);
                    pd.show();
                }
            }
        });

    }

    public static void dismissWaitDialog() {
        if (pd != null && (pd.isShowing())) {
            pd.dismiss();
        }
        pd = null;
    }
}
